//
//  FTowTime.swift
//  ThreadDriverText
//
//  Created by zhuxietong on 2021/3/8.
//  Copyright © 2021 Air. All rights reserved.
//

import Foundation
import Eelay
import DateTimePicker
import RxSwift


open class FDateTimeNode:FTime{
    
    open var mode:FForm.TimePickerMode = .day
    
    open var picker: UIDatePicker{
        return node.view as! UIDatePicker
    }
    public let node = ASDisplayNode { () -> UIView in
        return  UIDatePicker()
    }
    
    open override func addLayoutRules() {
        super.addLayoutRules()
        picker.minuteInterval = 5;
    }
    
    open override func updateDispaly() {
        super.updateDispaly()
        updateStyle()
    }
    
    open func updateStyle() {

        if let min = limit.min{
            picker.minimumDate = min
        }
        if let max = limit.max{
            picker.maximumDate = max
        }
        
        print("_ooo0000___S__S_S_S_S",format.pickerMode)

        switch format.pickerMode {
        case .day:
//            picker.isDatePickerOnly = true
            picker.datePickerMode = .date
            print("_ooo0000___S__S_S_S_S")
        case .hour:
            picker.datePickerMode = .dateAndTime
//            picker.includesMonth = true

        default:
            break
        }


        if #available(iOS 13.4, *) {
            picker.preferredDatePickerStyle = .wheels
        } else {
            // Fallback on earlier versions
        }
        if #available(iOS 13.4, *) {
        } else {
            // Fallback on earlier versions
        }
//        picker?.datePicker.date = Date(timeIntervalSinceNow: 0)

        picker.date = format.date ?? Date()

    }
    
    open override func specGetter() -> _Spec {
        return node["\(240.co)"]
    }
    
    
}




open class FTimeDialog:MoFullBodyDialog<FTimeDialog.Body>{

    public weak var fromTimeF:FTime? = nil
    public var date:Date? = nil
    
    public var selectDate:(_:Date?)->Void = {_ in}
    @discardableResult
    public func time(timeF:FTime?)->Self{
        fromTimeF = timeF
        return self
    }
    
    
    public override func viewDidLoad() {
        type = .sheet
        super.viewDidLoad()
        enabelBackTouch = true
        if let date_ = date{
            node.timeF.update(value: date_, edit: .fill)
        }
    }
    
    
    public class Body:DialogContent{
 
        public let confirmBt = ASButtonNode()
        public let cancelBt = ASButtonNode()
        public let titleL = ASTextNode2()
        public let timeF = FDateTimeNode(props: .name("timeMillis", "送达时间"),
                                  .format(FForm.TimeFormat(displayF: .dispay("YYYY-MM-dd HH:mm"), valueF: .millisecond_stamp, mode: .minute)),.placeholder("尽快送达"),.required(false))
       
        public override func addLayoutRules() {
            titleL.attributedText = "选择时间".ui.mfont16.center.color(color: ._foreground).lineSpace(5).astring
            cancelBt.ui.ntitle(title: "取消").font(size: 15).ncolor(color: ._weak).touchUpInside {[weak self] _ in
                self?.alert?.hidden()
            }
            confirmBt.ui.ntitle(title: "确定").font(size: 15).ncolor(color: ._tint).touchUpInside { [weak self] _ in
                self?.confirm()
            }
            
        }
        
        @objc open func confirm(){
            if let alert_ = alert as? FTimeDialog{
                alert_.selectDate(timeF.picker.date)
                alert_.fromTimeF?.update(value: timeF.picker.date, edit: .select)
            }
            alert?.hidden()
        }
        
  
        public override func mo_model_reload() {
            if let f = (alert as? FTimeDialog)?.fromTimeF{
                
                timeF.format = f.format
                timeF.limit = f.limit
                timeF.update(value: f.value, edit: .fill)
                titleL.attributedText = timeF.placeholder.ui.center.mfont16.astring
            }
        }
        
        public override func specGetter() -> _Spec {
            return .inset([], [14,0], child: .stack(["v|20",&-.stretch], childs: [
                .stack(["h|10",&-.center], childs: [
                    cancelBt["30",60],
                    titleL[_grow(1),_shrink(1)],
                    confirmBt["30",60],
                ]),
                timeF.Spec
            ]))
        }
    }
  
    
    
}



